package com.example.demo.config;

import com.example.demo.service.CustomUserDetailServiceImpl;
import com.example.demo.service.CustomUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.embedded.DataSourceFactory;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.data.repository.query.SecurityEvaluationContextExtension;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.provisioning.JdbcUserDetailsManager;
import org.springframework.security.provisioning.UserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;

import javax.sql.DataSource;
import java.sql.SQLException;

@Configuration
@ComponentScan
@EnableJpaRepositories
public class DataConfiguration {
//    @Bean
//    public SecurityEvaluationContextExtension expressionEvaluationContextProvider() throws SQLException {
//
//        System.out.println("securitydatea ext?????");
//        return new SecurityEvaluationContextExtension();
//    }

    // @formatter:off

    // @formatter:off
    @Autowired
    private DataSource dataSource;
    @Bean
    SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
        http
                .authorizeRequests(authorizeRequests -> authorizeRequests
                        .antMatchers("/password/*").permitAll()
                        .anyRequest().authenticated()
                )
                .formLogin(Customizer.withDefaults());
        return http.build();
    }


//    @Bean
//    UserDetailsService users() {
//        UserDetails user = User.builder()
//                .username("lengleng")
//                .password("{noop}123456")
//                .roles("USER")
//                .build();
//        return new InMemoryUserDetailsManager(user);
//    }

    @Bean
    UserDetailsManager users(DataSource dataSource){
        JdbcUserDetailsManager users= new JdbcUserDetailsManager(dataSource);
        UserDetails user1= User.builder().username("test1")
                .password("{noor}test1")
                .roles("USER")
                .build();
        users.createUser(user1);
        return users;
    }

    @Bean
    CustomUserDetailsService customUserDetailsService() {
        CustomUserDetailsService customUserDetailsService= new CustomUserDetailServiceImpl();
        return  customUserDetailsService;
    }





}
